<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>人物关系</title>

    <link rel="stylesheet" href="http://cdn.viptool.cn/lib/bootstrap/3.3.5/css/bootstrap.min.css">
    <link href="../../resource/hotui/css/font-awesome.min.css" th:href="@{/resource/hotui/css/font-awesome.min.css}"
          rel="stylesheet">
    <!-- Morris -->
    <link href="http://cdn.viptool.cn/lib/morris.js/0.4.3/morris.css" rel="stylesheet">
    <link href="../../resource/hotui/css/style.min-1.0.8.css" th:href="@{/resource/hotui/css/style.min-1.0.8.css}"
          rel="stylesheet">
    <link href="../../resource/hotui/css/hotui.style.override.css"
          th:href="@{/resource/hotui/css/hotui.style.override.css}" rel="stylesheet"/>


    <style>
        .data-statistics {
            padding: 10px 0;
        }

        .data-statistics span {
            display: inline-block;
            padding: 0 10px;
        }
    </style>
</head>

<body>
<form id="saveForm">
    <div class="wrapper wrapper-content tooltip-demo">
        <div class="row">
            <div class="col-lg-12">
                <div class="ibox float-e-margins m-b-none">
                    <div class="ibox-title" style="vertical-align: bottom;height: 100px;"
                         th:replace="fragments/user_detail_tab::userDetailHeader">
                    </div>
                    <div class="tabs-container">
                        <ul class="nav nav-tabs"
                            th:replace="fragments/user_detail_tab::userDetailTab(${user.userId},3)">
                        </ul>
                        <div class="tab-content">
                            <div class="tab-pane active">
                                <div class="panel-body">
                                    <div class="ibox float-e-margins">
                                        <div class="ibox-title">
                                            <h5 style="font-size: 18px;">
                                                当前用户： <span th:text="${user.mobile}">--</span>
                                                直推上线：<span th:if="${user.guider}!=null" th:text="${user.guider.mobile}">--</span>
                                            </h5>
                                            <!--                                            <button type="button"-->
                                            <!--                                                    onclick="userTreeHandler.modifyParent(this)"-->
                                            <!--                                                    class="btn btn-primary btn-xs m-b-none">更改上线-->
                                            <!--                                            </button>-->
                                        </div>
                                        <div class="data-statistics">
                                            <span>团队总人数:<strong th:text="${teamSummary.totalNum()}">1</strong></span>
                                            <span>团队普通粉丝总人数:<strong
                                                    th:text="${teamSummary.normal.count}">1</strong></span>
                                            <span>团队专属粉丝总人数:<strong
                                                    th:text="${teamSummary.buyer.count}">1</strong></span>
                                            <span>团队新秀总人数:<strong th:text="${teamSummary.vipCount}">1</strong></span>
                                            <span>团队达人总人数:<strong th:text="${teamSummary.drCount}">1</strong></span>
                                        </div>
                                        <div class="ibox-content">
                                            <table class="table table-bordered table-hover table-center">
                                                <thead>
                                                <tr>
                                                    <th>账户</th>
                                                    <th>手机号</th>
                                                    <th>状态</th>
                                                    <th>注册时间</th>
                                                    <th>操作</th>
                                                </tr>
                                                </thead>
                                                <tbody th:each="child:${children}">
                                                <tr id="child_1000" th:id="${'child_'+child.userId}">
                                                    <td style="text-align: left;">
                                                        <span th:text="${child.nickName}">王明</span>
                                                    </td>
                                                    <td th:text="${child.mobile}"></td>
                                                    <td>
                                                        <span class="color-primary"
                                                              th:text="${child.enabled?'激活':'冻结'}">激活</span>
                                                    </td>
                                                    <td th:text="${#temporals.format(child.createTime,'yyyy-MM-dd HH:mm:ss')}">
                                                        2018-09-30 22:47
                                                    </td>
                                                    <td>
                                                        <button th:onclick="|userTreeHandler.showChildren(${child.userId},1,this)|"
                                                                onclick="userTreeHandler.showChildren(1000,1,this)"
                                                                type="button" btn-status="0"
                                                                class="btn btn-white btn-xs m-b-none">查看下级
                                                        </button>
                                                        <a th:href="|javascript:userTreeHandler.userDetail(${child.userId},'${child.realName}')|"
                                                           href="javascript:userTreeHandler.userDetail(1000,'')">详细信息</a>
                                                    </td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<div id="modify_parent_modal" class="modal-content-tag">
    <div class="form-horizontal" style="width: 479px;">
        <div class="form-group form-inline">
            <label class="col-sm-4 control-label">当前推荐上线：</label>
            <div class="col-sm-8">
                <p class="form-control-static" style="color: #ff9966" id="oldParentName"></p>
            </div>
        </div>
        <div class="hr-line-dashed"></div>
        <div class="form-group">
            <label class="col-sm-4 control-label">更改为：</label>
            <div class="col-sm-8">
                <input type="text" required id="newParentName"
                       class="form-control input-sm input-s only-num" placeholder="手机号"/>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="children_template">
    <tr id="child_{{childId}}">
        <td style="text-align: left">
            <span style="margin-left: {{marginLeft}}">{{realName}}</span>
        </td>
        <td>{{username}}</td>
        <td>
            <span class="{{statusClass}}">{{status}}</span>
        </td>
        <td>{{createTime}}</td>
        <td>
            <button onclick="userTreeHandler.showChildren({{childId}},{{depth}},this)" type="button" btn-status="0"
                    class="btn btn-white btn-xs m-b-none">查看下级
            </button>
            <a href="javascript:userTreeHandler.userDetail({{childId}},'{{realName}}')">详细信息</a>
        </td>
    </tr>
</script>

<script src="http://cdn.viptool.cn/lib/jquery/2.2.4/jquery.min.js"></script>
<script src="http://cdn.viptool.cn/lib/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="../../resource/hotui/js/hot/bootstrap.hot.extra-utils.js?20190316"
        th:src="@{/resource/hotui/js/hot/bootstrap.hot.extra-utils.js?20190316(123123)}"></script>
<script src="../../resource/hotui/js/hot/bootstrap.hot.extra-init.js?20190316"></script>
<script src="../../resource/hotui/js/content.min.js" th:src="@{/resource/hotui/js/content.min.js}"></script>
<script type="text/javascript" th:inline="javascript">
    const userApiBase = /*[[@{/user}]]*/  "/user";
    const userDetailUri = /*[[@{/user/detail}]]*/  "/user";
    const userId = /*[[${user.userId}]]*/ 0;
</script>

<!--<script src="https://cdn.bootcss.com/Mock.js/1.0.0/mock-min.js"></script>-->
<!--<script src="../../mock/mock.user.js"></script>-->

<script type="text/javascript">

    var modifyParentModal = $("#modify_parent_modal").modal("修改推荐上线", function () {
        userTreeHandler.saveParent();
    });
    var userTreeHandler = {
        showChildren: function (userId, depth, obj) {
            let btnStatus = parseInt($(obj).attr("btn-status"));
            if (btnStatus === 0) {
                userTreeHandler.children(userId, 1, depth, obj);
            } else {
                $(obj).attr("btn-status", 0);
                $(obj).html("查看下级");
                $(".parent_" + userId).remove();
            }
        },
        children: function (userId, pageIndex, depth, obj) {
            hot.ajax(userApiBase + "/children", {
                userId: userId,
                pageIndex: pageIndex
            }, function (apiResult) {
                if (apiResult.resultCode === 2000) {
                    let resultList = apiResult.data;
                    if (resultList.length > 0) {
                        $(obj).attr("btn-status", 1);
                        $(obj).html("收起");
                        let childrenHtml = "";
                        $.each(resultList, function (o, item) {
                            let tempHtml = $("#children_template").html();
                            tempHtml = tempHtml.replace(/{{realName}}/g, item.nickName);
                            tempHtml = tempHtml.replace("{{username}}", item.username);
                            tempHtml = tempHtml.replace("{{authCode}}", item.authCode);
                            tempHtml = tempHtml.replace("{{status}}", item.enabled ? "激活" : "禁用");
                            tempHtml = tempHtml.replace("{{statusClass}}", item.enabled ? "color-warning" : "color-danger");
                            tempHtml = tempHtml.replace("{{createTime}}", item.createTime);
                            tempHtml = tempHtml.replace(/{{childId}}/g, item.userId);
                            tempHtml = tempHtml.replace("{{depth}}", depth + 1);
                            tempHtml = tempHtml.replace("{{marginLeft}}", depth * 15 + 'px');

                            childrenHtml += tempHtml;
                        });
                        let parentClasses = $("#child_" + userId).attr("class");
                        let $child = $(childrenHtml);
                        $child.attr("class", parentClasses);
                        $child.addClass('parent_' + userId);

                        $("#child_" + userId).after($child);
                    } else {
                        hot.tip.msg("当前用户没有下级");
                    }

                }
            }, function () {
            }, "post");
        },
        userDetail: function (userId, realName) {
            let url = userDetailUri + "?userId=" + userId;
            hot.newTab(url, realName)
        },
        modifyParent: function () {
            modifyParentModal.show(function () {
                $('#oldParentName').html($('.js-parent-uinfo').html());
            });
        },
        saveParent: function () {
            if ($('#newParentName').val() == '') {
                hot.tip.error('请输入');
                return;
            }
            var param = {userId: userId, newParentName: $('#newParentName').val()};
            hot.ajax('/user/changeParent', param, function (ret) {
                hot.loading.close();
                if (ret.resultCode === 2000) {
                    hot.tip.success('更改成功');
                    setTimeout(function () {
                        window.location.reload();
                    }, 800);
                } else {
                    hot.tip.error('更改失败：' + ret.resultMsg);
                }
            }, function () {
            }, "post");
        },
    }
</script>
</body>

</html>
